<%--
  Created by IntelliJ IDEA.
  User: 孤焰
  Date: 2021/6/17
  Time: 17:50
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">用户编辑</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input value="${userInfo.username}" readonly="readonly" type="text" name="username" lay-verify="username" autocomplete="off" placeholder="请输入用户名"
                               class="layui-input layui-disabled">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" value="${userInfo.password}" lay-verify="password" autocomplete="off" placeholder="请输入密码"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">确认密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="passwordForm" value="${userInfo.password}" lay-verify="required" autocomplete="off" placeholder="请输入密码"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" value="${userInfo.email}" lay-verify="email" autocomplete="off" placeholder="请输入邮箱"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">手机</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" value="${userInfo.phone}" lay-verify="phone" autocomplete="off" placeholder="请输入手机号码"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="user-submit">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/static/js/axios.min.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function () {
        var layer = layui.layer;
        var form = layui.form;

        // 自定义表单验证
        form.verify({
            username: [/^.{5,}$/, '用户名至少有5位字符'],
            password: [/(.+){6,12}$/, '密码必须6-12位']
        });

        /* 监听提交 */
        form.on('submit(user-submit)', function (data) {
            var user = data.field;
            user.id = '${userInfo.id}';

            if (user.password !== user.passwordForm) {
                layer.msg('两次输入密码不一致', {icon: 5});
                return false;
            }

            delete user.passwordForm;

            axios.post('${pageContext.request.contextPath}/user/updateUserById', user).then(function (res) {
                if (res.data.code == 200) {
                    layer.msg("数据更新成功", {icon: 6});
                    setTimeout(function () {
                        parent.location.reload();// 刷新父窗口
                        window.parent.layer.closeAll();//关闭弹窗
                    }, 800);
                } else {
                    layer.msg(res.data.message, {icon: 5});
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
